package com.gzone.contentprovider.sql.dao;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.database.Cursor;
import android.util.Log;

import com.gzone.contentprovider.helper.BitmapHelper;
import com.gzone.contentprovider.helper.DataHelper;
import com.gzone.contentprovider.sql.model.Category;
import com.gzone.contentprovider.sql.model.Item;
import com.gzone.contentprovider.sql.model.Selection;
import com.gzone.contentprovider.utils.HTTPClientUtil;

public class ItemDAO implements DAO<Item> {

	private static final String TABLE_NAME = "Item";
	private static final String ID = "Item_ID";
	public static final String CATEGORY_ID = "Item_category_ID";
	private static final String TITLE = "Item_title";
	private static final String PRICE = "Item_price";
	private static final String DESC = "Item_desc";
	private static final String IMG = "Item_img";
	private static final String ICON = "Item_icon";
	private static final String ORDER = "Item_order";
	private static final String LIKE = "Item_like";
	private static final String SYNCDATE = "Item_syncdate";
	public ItemDAO() {
		DataHelper.dropTable(TABLE_NAME);
		String tableSql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ("
				+ ID + " INTEGER PRIMARY KEY," 
				+ CATEGORY_ID + " INTEGER,"
				+ TITLE + " TEXT," 
				+ DESC + " TEXT," 
				+ PRICE + " TEXT," 
				+ IMG + " BLOB NULL," 
				+ ICON + " BLOB NULL," 
				+ ORDER + " INTEGER," 
				+ LIKE + " INTEGER,"
				+ SYNCDATE + " TEXT" + ");";
		DataHelper.createTable(tableSql);
	}

	public void delete(Item object) {
		// TODO Auto-generated method stub
		DataHelper.delete(TABLE_NAME, object.id);
	}
	
	@Override
	public void deleteAllRow() {
		DataHelper.deleteAllRow(TABLE_NAME);
	}

	@Override
	public ArrayList<Item> findAll() {
		// TODO Auto-generated method stub
		Cursor data = DataHelper.readAll(TABLE_NAME);
		ArrayList<Item> list = new ArrayList<Item>();
		if (data.getCount() > 0) {
			data.moveToFirst();
			int id = data.getColumnIndex(ID);
			int category_id = data.getColumnIndex(CATEGORY_ID);
			int title = data.getColumnIndex(TITLE);
			int desc = data.getColumnIndex(DESC);
			int price = data.getColumnIndex(PRICE);
			int img = data.getColumnIndex(IMG);
			int icon = data.getColumnIndex(ICON);
			int order = data.getColumnIndex(ORDER);
			int like = data.getColumnIndex(LIKE);
			int syncdate = data.getColumnIndex(SYNCDATE);
			do {
				Item obj = new Item();
				obj.id = data.getInt(id);
				obj.category_id = data.getInt(category_id);
				obj.title = data.getString(title);
				obj.desc = data.getString(desc);
				obj.price = data.getString(price);
				obj.img = data.getBlob(img);
				obj.icon = data.getBlob(icon);
				obj.order = data.getInt(order);
				obj.like = data.getInt(like);
				obj.syncdate = data.getString(syncdate);
				list.add(obj);
			} while (data.moveToNext());
		}
		data.close();
		return list;
	}

	@Override
	public Item findById(int id) {
		Cursor data = DataHelper.findByParam(TABLE_NAME, ID, id);
		Item obj = null;
		if (data.getCount() > 0) {
			data.moveToFirst();
			int iid = data.getColumnIndex(ID);
			int category_id = data.getColumnIndex(CATEGORY_ID);
			int title = data.getColumnIndex(TITLE);
			int desc = data.getColumnIndex(DESC);
			int price = data.getColumnIndex(PRICE);
			int img = data.getColumnIndex(IMG);
			int icon = data.getColumnIndex(ICON);
			int order = data.getColumnIndex(ORDER);
			int like = data.getColumnIndex(LIKE);
			int syncdate = data.getColumnIndex(SYNCDATE);
			obj = new Item();
			obj.id = data.getInt(iid);
			obj.category_id = data.getInt(category_id);
			obj.title = data.getString(title);
			obj.desc = data.getString(desc);
			obj.price = data.getString(price);
			obj.img = data.getBlob(img);
			obj.icon = data.getBlob(icon);
			obj.order = data.getInt(order);
			obj.like = data.getInt(like);
			obj.syncdate = data.getString(syncdate);
		}
		data.close();
		return obj;
	}

	@Override
	public ArrayList<Item> findByParameter(String paraName, Object value) {
		Cursor data = DataHelper.findByParam(TABLE_NAME, paraName, value);
		ArrayList<Item> list = new ArrayList<Item>();
		if (data.getCount() > 0) {
			data.moveToFirst();
			int id = data.getColumnIndex(ID);
			int category_id = data.getColumnIndex(CATEGORY_ID);
			int title = data.getColumnIndex(TITLE);
			int desc = data.getColumnIndex(DESC);
			int price = data.getColumnIndex(PRICE);
			int img = data.getColumnIndex(IMG);
			int icon = data.getColumnIndex(ICON);
			int order = data.getColumnIndex(ORDER);
			int like = data.getColumnIndex(LIKE);
			int syncdate = data.getColumnIndex(SYNCDATE);
			do {
				Item obj = new Item();
				obj.id = data.getInt(id);
				obj.category_id = data.getInt(category_id);
				obj.title = data.getString(title);
				obj.desc = data.getString(desc);
				obj.price = data.getString(price);
				obj.img = data.getBlob(img);
				obj.icon = data.getBlob(icon);
				obj.order = data.getInt(order);
				obj.like = data.getInt(like);
				obj.syncdate = data.getString(syncdate);
				list.add(obj);
			} while (data.moveToNext());
		}
		data.close();
		return list;
	}

	@Override
	public void insert(Item object) {
		// TODO Auto-generated method stub
		object.img = HTTPClientUtil.getImage(object.imgURL);
		object.icon =  HTTPClientUtil.getImage(object.iconURL);
//		if(object.icon == null) object.icon = "null".getBytes();
		Object[] bindArgs = {object.id, object.category_id, object.title, 
				object.desc,object.price, object.img, object.icon, 
				object.order, object.like, object.syncdate};
		String insert = "INSERT INTO " + TABLE_NAME + " ("
		+ ID + "," 
		+ CATEGORY_ID + ","
		+ TITLE + "," 
		+ DESC + "," 
		+ PRICE + "," 
		+ IMG + "," 
		+ ICON + "," 
		+ ORDER + "," 
		+ LIKE + ","
		+ SYNCDATE + "" + ") VALUES (?,?,?,?,?,?,?,?,?,?);";
		Log.i("Insert", "---> " + insert);
		DataHelper.insert(insert, bindArgs);
	}

	@Override
	public void update(Item object) {
		// TODO Auto-generated method stub
		object.img = BitmapHelper.getByteImageFromURL(object.imgURL);
		object.icon = BitmapHelper.getByteImageFromURL(object.iconURL);
		Object[] bindArgs = {object.category_id, object.title, 
				object.desc, object.price,object.img, object.icon, 
				object.order, object.like, object.syncdate, object.id};
		String update = "UPDATE '" + TABLE_NAME + "' SET "
		+ CATEGORY_ID + "=?,"
		+ TITLE + "=?," 
		+ DESC + "=?," 
		+ PRICE + "=?," 
		+ IMG + "=?," 
		+ ICON + "=?," 
		+ ORDER + "=?," 
		+ LIKE + "=?,"
		+ SYNCDATE + "=?"
		+ " WHERE " + ID + "=?;"; 
		DataHelper.update(update, bindArgs);
	}

	@Override
	public ArrayList<Item> findAllToUpdate() {
		String query = "SELECT " + ID + ", " + SYNCDATE + " FROM '" + TABLE_NAME +"'";
		Cursor data = DataHelper.rawQuery(query, null);
		ArrayList<Item> list = new ArrayList<Item>();
		if (data.getCount() > 0) {
			data.moveToFirst();
			int id = data.getColumnIndex(ID);
			int syncdate = data.getColumnIndex(SYNCDATE);
			do {
				Item obj = new Item();
				obj.id = data.getInt(id);
				obj.syncdate = data.getString(syncdate);
				list.add(obj);
			} while (data.moveToNext());
		}
		data.close();
		return list;
	}

	@Override
	public ArrayList<Item> readJSONData(String data) {
		ArrayList<Item> list = new ArrayList<Item>();
		try {
			JSONObject jSon = new JSONObject(data);
			System.out.println("---> json events: " + jSon);
			JSONArray arr = jSon.getJSONArray("item");
			JSONObject tmp;
			for(int i = 0; i < arr.length(); i++){
				tmp = arr.getJSONObject(i);
				Item obj = new Item();
				obj.id = tmp.getInt(ID);
				obj.category_id = tmp.getInt(CATEGORY_ID);
				obj.title = tmp.getString(TITLE);
				obj.desc = tmp.getString(DESC);
				obj.imgURL = tmp.getString(IMG);
				obj.iconURL = tmp.getString(ICON);
				obj.order = tmp.getInt(ORDER);
				obj.price = tmp.getString(PRICE);
				obj.like = tmp.getInt(LIKE);
				obj.syncdate = tmp.getString(SYNCDATE);
				list.add(obj);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

}
